package com.qffx.moviebase.presenter;

import android.util.Log;

import com.qffx.moviebase.bean.RecommendEntity;
import com.qffx.moviebase.model.RecommendModel;
import com.qffx.moviebase.model.impl.RecommendM;
import com.qffx.moviebase.presenter.impl.BasePresenter;
import com.qffx.moviebase.utils.json.RecommendJSON;
import com.qffx.moviebase.view.impl.IRecommendView;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 韩爽 on 2017/2/7.
 */

public class RecommendPresenter implements BasePresenter {
   private RecommendM recommendM;
   private IRecommendView iRecommendView;
   public static final String TAG="RecommendPresenter";

   public RecommendPresenter(IRecommendView iRecommendView) {
      this.iRecommendView = iRecommendView;
      recommendM=new RecommendModel();
   }

   @Override
   public void getData() {
      Observable<String> data = recommendM.loadData();
      data
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .map(new Func1<String, RecommendEntity>() {
                 //利用rxjava中的map将string转化成我想要的数据
                 @Override
                 public RecommendEntity call(String s) {
                    RecommendEntity recomJSON = RecommendJSON.getRecomJSON(s);
                    return recomJSON;
                 }
              }).subscribe(new Subscriber<RecommendEntity>() {
         @Override
         public void onCompleted() {
            Log.e(TAG, "onCompleted: 完成" );
         }

         @Override
         public void onError(Throwable e) {
            Log.e(TAG, "onError: "+e.getMessage());
         }

         @Override
         public void onNext(RecommendEntity recommendEntity) {
              iRecommendView.setData(recommendEntity);
         }
      });
   }
}
